package smarthouse;

import java.lang.reflect.Field;
import java.util.Vector;

import annotations.Command;
import annotations.Device;
import annotations.Param;

import devices.AbstractDevice;

public class SmarthouseXMLEnc {
	StringBuffer xml = new StringBuffer();
	
	public String encode(Vector<Vector<AbstractDevice>> devicesByRoom){

		
		Room[] rooms = Room.values();
		int tabs = 0;
		
		xml.append("<smarthouse>\n");
		
		tabs++;
		for(int i=0;i<rooms.length;i++){
			append(tabs,"<room name=\""+rooms[i].getName()+"\">");
			
			Vector<AbstractDevice> devices = devicesByRoom.get(i);
			int size = devices.size();
			tabs++;
			for(int j=0;j<size;j++){
				AbstractDevice device = devices.get(j);
				
				Device deviceAnnotation = (Device) device.getClass().getAnnotation(Device.class);
				String deviceName = deviceAnnotation.name();
				String deviceDescription = deviceAnnotation.description();
				String deviceIconURL = deviceAnnotation.iconURL();
				
				
				append(tabs, "<device name=\""+deviceName+"\" description=\""+deviceDescription+"\">");
				
				Vector<Class<?>> commandClasses = device.getCommands();
				int size2 = commandClasses.size();
				tabs++;
				for (int k = 0; k < size2; k++) {
					Class<?> commandClass = commandClasses.get(k);
					
					Command commandAnnotation = (Command) commandClass.getAnnotation(Command.class);
					String commandName = commandAnnotation.name();
					String commandDescription = commandAnnotation.description();
					String commandIconURL = commandAnnotation.iconURL();	
					int commandID=commandAnnotation.id();
					
					append(tabs, "<command id=\""+commandID+"\" name=\""+commandName+"\" description=\""+commandDescription+"\">");
				
				
					Field[] fields= commandClass.getDeclaredFields();
					Field field = null;
					
					tabs++;
					for (int l = 0; l < fields.length; l++) {
						field = fields[l];
						if (!field.isAnnotationPresent(Param.class)) continue;
						
						Param p =field.getAnnotation(Param.class);
						Class<?> paramType = field.getType();
						String paramName = p.name();
						String paramDescription = p.description();
						int paramId = p.id();
						int paramLength = p.length();
						boolean paramMandatory = p.mandatory();
						boolean paramPercentage = p.percentage();
						
						append(tabs, "<param name=\""+paramName+"\" description=\""+paramDescription+"\" id=\""+paramId+"\" type=\""+paramType+"\">");
						
						tabs++;
						append(tabs, "<length>"+paramLength+"</length>");
						append(tabs, "<mandatory>"+paramMandatory+"</mandatory>");
						append(tabs, "<percentage>"+paramPercentage+"</percentage>");
						
						tabs--;
						append(tabs, "</param>");
					}
					tabs--;
					append(tabs, "</command>");
				
				}
				tabs--;
				append(tabs, "</device>");
			}
			tabs--;
			append(tabs, "</room>");
		}
		
		tabs--;
		append(tabs, "</smarthouse>");
		
		
		
		return xml.toString();
		
	}
	
	private void append(int tabs, String value){
		for (;tabs-->0;) {
			xml.append("\t");
		}
		xml.append(value);
		xml.append("\n");
	}

}
